function toListTopic() {
    var c = confirm("Are you sure?");
    if (c)
        document.location = "/admin/listtopic";
}

function addMoreInfor() {
    var moreInfo = {};
    moreInfo.title = $("#newMoreInfoTitle").val();
    moreInfo.link = $("#newMoreInfoLink").val();
    $("#moreInfo_tpl").tmpl(moreInfo).appendTo( "#allMoreInfo_id" );

    reCalMoreInfoAsJson();

    //reset input value
    $("#newMoreInfoTitle").val("");
    $("#newMoreInfoLink").val("");
}

function delMoreInfo(node) {
    var ul = node.parentNode.parentNode;
    var container = ul.parentNode.removeChild(ul);

    reCalMoreInfoAsJson();
}

function reCalMoreInfoAsJson() {
    var more = [];
    $(".ul_moreInfo").each(function(i,ul) {
        var oneMore={};
        //oneMore.id = parseInt(ul.title);

        var lis = $(ul).find("li");
        oneMore.title = $.trim(lis[0].innerHTML);
        oneMore.link = $.trim(lis[1].innerHTML);

        more.push(oneMore);
    })

    var moreAsStr = $.toJSON(more);
    console.log(moreAsStr);
    $("#moreInfoAsJson_id").val(moreAsStr);
}


function addRelated() {
    var data = {};
    var selected = $("#select_related_id option:selected");
    data.topicId = selected.val();
    data.code = selected.text();

    $("#related_tpl").tmpl(data).appendTo( "#allRelatedTopic_id" );

    reCalRelatedTopicAsJson();
}

function delRelated(node) {
    var ul = node.parentNode.parentNode;
    var container = ul.parentNode.removeChild(ul);

    reCalRelatedTopicAsJson();
}

function reCalRelatedTopicAsJson() {
    var more = [];
    $(".ul_related").each(function(i,ul) {
        var oneMore={};
        //oneMore.id = parseInt(ul.title);

        var lis = $(ul).find("li");
        oneMore.topicId = $.trim(lis[0].innerHTML);
        oneMore.code = $.trim(lis[1].innerHTML);

        more.push(oneMore);
    })

    var moreAsStr = $.toJSON(more);
    console.log(moreAsStr);
    $("#relatedTopicsAsJson_id").val(moreAsStr);
}


